﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Diagnostics;
using System.Windows.Controls.Primitives;

namespace System.Windows.BidiControls
{
    [TemplatePart(Name = "TemplateBottomSelected", Type = typeof(FrameworkElement)), TemplatePart(Name = "TemplateRightUnselected", Type = typeof(FrameworkElement)), TemplateVisualState(Name = "Focused", GroupName = "FocusStates"), TemplatePart(Name = "HeaderBottomSelected", Type = typeof(ContentControl)), TemplatePart(Name = "HeaderLeftSelected", Type = typeof(ContentControl)), TemplateVisualState(Name = "Unfocused", GroupName = "FocusStates"), TemplatePart(Name = "TemplateTopSelected", Type = typeof(FrameworkElement)), TemplatePart(Name = "TemplateLeftSelected", Type = typeof(FrameworkElement)), TemplatePart(Name = "TemplateRightSelected", Type = typeof(FrameworkElement)), TemplatePart(Name = "TemplateTopUnselected", Type = typeof(FrameworkElement)), TemplatePart(Name = "TemplateBottomUnselected", Type = typeof(FrameworkElement)), TemplatePart(Name = "TemplateLeftUnselected", Type = typeof(FrameworkElement)), TemplatePart(Name = "HeaderTopSelected", Type = typeof(ContentControl)), TemplatePart(Name = "HeaderRightUnselected", Type = typeof(ContentControl)), TemplateVisualState(Name = "Selected", GroupName = "SelectionStates"), TemplatePart(Name = "HeaderTopUnselected", Type = typeof(ContentControl)), TemplatePart(Name = "HeaderBottomUnselected", Type = typeof(ContentControl)), TemplatePart(Name = "HeaderLeftUnselected", Type = typeof(ContentControl)), TemplatePart(Name = "HeaderRightSelected", Type = typeof(ContentControl)), TemplateVisualState(Name = "Normal", GroupName = "CommonStates"), TemplateVisualState(Name = "MouseOver", GroupName = "CommonStates"), TemplateVisualState(Name = "Disabled", GroupName = "CommonStates"), TemplateVisualState(Name = "Unselected", GroupName = "SelectionStates")]
    public class TabItem : System.Windows.Controls.TabItem
    {
        private FrameworkElement ElementTemplateTopSelected { get; set; }
        private FrameworkElement ElementTemplateBottomSelected { get; set; }
        private FrameworkElement ElementTemplateLeftSelected { get; set; }
        private FrameworkElement ElementTemplateRightSelected { get; set; }
        private FrameworkElement ElementTemplateTopUnselected { get; set; }
        private FrameworkElement ElementTemplateBottomUnselected { get; set; }
        private FrameworkElement ElementTemplateLeftUnselected { get; set; }
        private FrameworkElement ElementTemplateRightUnselected { get; set; }
        private ContentControl _contentWrapper = new ContentControl();
        private System.Windows.Controls.ContentControl ElementHeaderTopSelected { get; set; }
        private System.Windows.Controls.ContentControl ElementHeaderBottomSelected { get; set; }
        private System.Windows.Controls.ContentControl ElementHeaderLeftSelected { get; set; }
        private System.Windows.Controls.ContentControl ElementHeaderRightSelected { get; set; }
        private System.Windows.Controls.ContentControl ElementHeaderTopUnselected { get; set; }
        private System.Windows.Controls.ContentControl ElementHeaderBottomUnselected { get; set; }
        private System.Windows.Controls.ContentControl ElementHeaderLeftUnselected { get; set; }
        private System.Windows.Controls.ContentControl ElementHeaderRightUnselected { get; set; }

        private bool _isTemplateApplied = false;
        
        public TabItem()
        {
            DefaultStyleKey = typeof(TabItem);
            _contentWrapper.FlowDirection = FlowDirection.RightToLeft;
            //DataContext = this.Content;
        }

        public override void OnApplyTemplate()
        {
            _isTemplateApplied = true;
            base.OnApplyTemplate();
            System.Windows.Controls.ContentControl contentControl = this.GetContentControl(this.IsSelected, this.TabStripPlacement);
            if (contentControl != null)
            {
                contentControl.Content = null;
            }
            this.ElementTemplateTopSelected = base.GetTemplateChild("TemplateTopSelected") as FrameworkElement;
            this.ElementTemplateBottomSelected = base.GetTemplateChild("TemplateBottomSelected") as FrameworkElement;
            this.ElementTemplateLeftSelected = base.GetTemplateChild("TemplateLeftSelected") as FrameworkElement;
            this.ElementTemplateRightSelected = base.GetTemplateChild("TemplateRightSelected") as FrameworkElement;
            this.ElementTemplateTopUnselected = base.GetTemplateChild("TemplateTopUnselected") as FrameworkElement;
            this.ElementTemplateBottomUnselected = base.GetTemplateChild("TemplateBottomUnselected") as FrameworkElement;
            this.ElementTemplateLeftUnselected = base.GetTemplateChild("TemplateLeftUnselected") as FrameworkElement;
            this.ElementTemplateRightUnselected = base.GetTemplateChild("TemplateRightUnselected") as FrameworkElement;
            this.ElementHeaderTopSelected = base.GetTemplateChild("HeaderTopSelected") as System.Windows.Controls.ContentControl;
            this.ElementHeaderBottomSelected = base.GetTemplateChild("HeaderBottomSelected") as System.Windows.Controls.ContentControl;
            this.ElementHeaderLeftSelected = base.GetTemplateChild("HeaderLeftSelected") as System.Windows.Controls.ContentControl;
            this.ElementHeaderRightSelected = base.GetTemplateChild("HeaderRightSelected") as System.Windows.Controls.ContentControl;
            this.ElementHeaderTopUnselected = base.GetTemplateChild("HeaderTopUnselected") as System.Windows.Controls.ContentControl;
            this.ElementHeaderBottomUnselected = base.GetTemplateChild("HeaderBottomUnselected") as System.Windows.Controls.ContentControl;
            this.ElementHeaderLeftUnselected = base.GetTemplateChild("HeaderLeftUnselected") as System.Windows.Controls.ContentControl;
            this.ElementHeaderRightUnselected = base.GetTemplateChild("HeaderRightUnselected") as System.Windows.Controls.ContentControl;

            this.OnHeaderChanged(null, this.Header);
        }
        object _oldHeader;
        protected override void OnHeaderChanged(object oldHeader, object newHeader)
        {
            //if (!_isTemplateApplied)
            //    this.OnApplyTemplate();
            System.Windows.Controls.ContentControl contentControl = this.GetContentControl(this.IsSelected, this.TabStripPlacement);
            if (contentControl != null)
            {
                if (_oldHeader != newHeader)
                {
                    _oldHeader = newHeader;
                    if(newHeader is string)
                    {
                        _oldHeader = NBidi.NBidi.LogicalToVisual(newHeader as string);
                        this.Header = _oldHeader;
                        return;
                    }
                }
                if (this.HeaderTemplate == null)
                {
                    //_contentWrapper.Content = this.Header;
                    contentControl.Content = this.Header;
                }
                else
                {
                    FrameworkElement element = this.HeaderTemplate.LoadContent() as FrameworkElement;
                    contentControl.Content = element;
                }
            }

        }

        internal System.Windows.Controls.ContentControl GetContentControl(bool isSelected, System.Windows.Controls.Dock tabPlacement)
        {
            switch (tabPlacement)
            {
                case System.Windows.Controls.Dock.Left:
                    if (isSelected)
                    {
                        return this.ElementHeaderLeftSelected;
                    }
                    return this.ElementHeaderLeftUnselected;

                case System.Windows.Controls.Dock.Top:
                    if (isSelected)
                    {
                        return this.ElementHeaderTopSelected;
                    }
                    return this.ElementHeaderTopUnselected;

                case System.Windows.Controls.Dock.Right:
                    if (isSelected)
                    {
                        return this.ElementHeaderRightSelected;
                    }
                    return this.ElementHeaderRightUnselected;

                case System.Windows.Controls.Dock.Bottom:
                    if (isSelected)
                    {
                        return this.ElementHeaderBottomSelected;
                    }
                    return this.ElementHeaderBottomUnselected;
            }
            return null;
        }


    }
}
